package GameLogic
{
	import BasicEngine.Dispatcher;
	import BasicEngine.Vector2D;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import LoadUnit.LoadManager;
	import luaAlchemy.LuaAlchemy;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class Game
	{
		/**
		 * 主容器
		 */
		public static var MainCanvas:Sprite;
		/**
		 * 地图容器
		 */
		public static var Map:Sprite;
		/**
		 * 菜单容器
		 */
		public static var MenuCanvas:Sprite;
		/**
		 * Player容器
		 */
		public static var PlayerCanvas:Sprite;
		/**
		 * 唯一的主角
		 */
		public static var LeadCanvas:Sprite;
		/**
		 * 建筑容器
		 */
		public static var Building:Sprite;
		/**
		 * 天空容器
		 */
		public static var SkyCanvas:Sprite;
		
		public static var MainTimer:Timer;			
		public static var Pause:Boolean;
		public static var Width:int;
		public static var Height:int;		
		public static var MainStage:Stage;
		
		public static var NEXT_ID:int = 1000;
        public static var LuaAl:LuaAlchemy;		
		public static var LuaDict:Dictionary;
		public static var GraphicsTool:Graphics;
		
		public static var ResourceReady:Boolean = false;
		public static var OpenLuaEditBtn:SpriteBtn;
		public static var LuaEdit:LuaEditer;
		public static var GoAndPlayBtn:PauseBtn;
		public static var InfoShowBtn:InfoShow;
		public static var PrintInfo:TraceWin;
		public static var Robothead:Head;
		
		public static var ShowRadar:LargeRadar;
		
		public function Game()
		{
		}
		
		//初始化
		public static function Init(_main:Sprite,_graphics:Graphics,_width:int,_height:int):void
		{
			Dispatcher.Init();
			InitCanvas(_main);
			
			LuaAl = new LuaAlchemy();
			LuaAl.setGlobal("this", _main);
			LuaDict = new Dictionary();
			Pause = true;
			Width = _width;
			Height = _height;
			GraphicsTool = _graphics;
			MainTimer = new Timer(20,0); //20 毫秒计时一次
			MainTimer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent) :void
			{
				Update();
			});		
		}
		
		
		public static function Update():void
		{
			if (!Pause)
			{
				Dispatcher.ExecuteUpdate();
				LoadManager.Update();
			}
		}
		
		//游戏启动，启动游戏调度,启动循环
		public static function Star():void
		{
			MainTimer.start();
			Pause = false;
		}
		
		public static function WorldToLocal(_global:Vector2D,_localCoordinatorOrigin:Vector2D,_localCoordinatorHead:Vector2D):Vector2D
		{
			var local:Vector2D =  new Vector2D(_global.X - _localCoordinatorOrigin.X, _global.Y - _localCoordinatorOrigin.Y);
			var angle:Number = local.Angle - _localCoordinatorHead.Angle;
			var re:Vector2D = new Vector2D(0, 0);
			re.Length = local.Length;
			re.Angle = angle;
			return re;
		}
		
		public static function LocalToWorld(_local:Vector2D, _localCoordinatorOrigin:Vector2D, _localCoordinatorHead:Vector2D):Vector2D
		{
			var v:Vector2D = _local.Clone();
			v.Angle = _local.Angle + _localCoordinatorHead.Angle;
			v.X = v.X + _localCoordinatorOrigin.X;
			v.Y = v.Y + _localCoordinatorOrigin.Y;
			return v;
		}
		
		public static function ImgResourceReady():void
		{
			ResourceReady = true;
		}
		
		public static function OutputTrace(_st:String):void
		{
			LuaEdit.OutputWin.text += _st;
		}
		
		/**
		 * 初始化容器
		 */
		private static function InitCanvas(_root:Sprite):void
		{
			MainCanvas = _root;
		    Map = new Sprite();
			MenuCanvas = new Sprite();
			PlayerCanvas = new Sprite();
			LeadCanvas = new Sprite();
			Building = new Sprite();
			SkyCanvas = new Sprite();
			
			MainCanvas.addChild(Map);
			MainCanvas.addChild(PlayerCanvas);
			MainCanvas.addChild(LeadCanvas);
			MainCanvas.addChild(Building);
			MainCanvas.addChild(SkyCanvas);
			MainCanvas.addChild(MenuCanvas);
		}
	}
}